import java.util.ArrayList;


public class SearchView extends View implements RectangularButtonDelegate{

	private TextBox searchBox;
	private ArrayList<Product> searchFew;
	private ResultArea  resultArea;
    private RectangularButton nextResults, previousResults;
    private int startIndex, endIndex;
    
	public SearchView(float x_, float y_, float w_, float h_, MainView p) {
		super(x_, y_, w_, h_, p);
		searchBox = new TextBox(0,0,450, 20,p,"Search");
		this.startIndex  = 0;
		this.endIndex = 18;
		this.subviews = new ArrayList<View>();
		this.resultArea = new ResultArea(0,20,450,h-20,p);
		this.subviews.add(resultArea);
		this.subviews.add(searchBox);
		this.previousResults = new RectangularButton(120, h-50, 100, 30,parent,1);
		this.previousResults.setCaption("Previous");
		this.nextResults = new RectangularButton(230, h-50, 100, 30,parent,2);
		this.nextResults.setCaption("Next");
		//this.subviews.add(nextResults);
		this.nextResults.setDelegate(this);
		//this.subviews.add(previousResults);
		this.previousResults.setDelegate(this);
	}
	/*
	void drawContent()
	  {
		//System.out.println()
		//parent.fill(255);
		//parent.rect(0,0,w,h);
		
		
	  }*/
	 boolean toggleSearchBox(){
		 
		 return searchBox.toggleActivated();
	 }
	 boolean mousePressed(float px, float py)
	  {
		 if (!ptInRect(px, py, x, y, w, h)) {
			 searchBox.setActivated(false);
			 return false;}
		    float lx = px - x;
		    float ly = py - y;
		    // check our subviews first
		    for (int i = subviews.size()-1; i >= 0; i--) {
		      View v = (View)subviews.get(i);
		      if(v.equals(searchBox)){
		    	  if (v.mousePressed(lx, ly)) return true;
		    	  else
		    		  searchBox.setActivated(false);
		      }
		      else
		      if (v.mousePressed(lx, ly)) return true;
		    }
		    return contentPressed(lx, ly);
	  }
	  public void keyPressed(char key) 
	  {
		  if(this.searchBox.getActivated()){
		  resultArea.subviews = new ArrayList<View>();
		  searchBox.keyPressed(key);
		  System.out.println("Key no = "+(int)key+" Current Search text = "+ searchBox.getText());
		  searchFew = new ArrayList<Product>();
		  for(int i = 0; i < parent.productsList.size(); i++){
				if(parent.productsList.get(i). getProductName().toLowerCase().contains(this.searchBox.getText().toLowerCase()))
					searchFew.add(parent.productsList.get(i));
			}
			
		  int y = 0;
			for(int i = 0; i < searchFew.size() && i < 18; i++){
				//System.out.println(searchFew.get(i).getProductName());
				resultArea.subviews.add(new ResultBox(0,y,w,30,parent,searchFew.get(i)));
				y += 30;
			}	
			  
			if(searchFew.size() > 18)
				this.subviews.add(nextResults);
		  }
	  }
	  public void RectangularButtonPressed( int id){
			if(id == 1){
				//if(startIndex > 0){
					endIndex = startIndex;
					startIndex = startIndex - 18;
					if(startIndex <0)
						startIndex = 0;
				//}

					System.out.println("start = "+startIndex +" end = "+ endIndex);
					int y = 0;
				for(int i = startIndex; i < searchFew.size() && i < endIndex; i++){
					//System.out.println(searchFew.get(i).getProductName());
					resultArea.subviews.add(new ResultBox(0,y,w,30,parent,searchFew.get(i)));
					System.out.println("Index printed = "+ i);
					y += 30;
				}	
				if(startIndex == 0){
					if(this.subviews.contains(previousResults)) this.subviews.remove(previousResults);
					
				}
				if(!this.subviews.contains(nextResults)) this.subviews.add(nextResults);
			}
			if(id == 2){
				// Next
				startIndex = endIndex;
				endIndex = endIndex + 18;
				if(endIndex > searchFew.size())
					endIndex = searchFew.size();
				System.out.println("start = "+startIndex +" end = "+ endIndex);
				int y  = 0;
				for(int i = startIndex; i < searchFew.size() && i < endIndex; i++){
					//System.out.println(searchFew.get(i).getProductName());
					resultArea.subviews.add(new ResultBox(0,y,w,30,parent,searchFew.get(i)));
					System.out.println("Index printed = "+ i);
					y += 30;
				}	
				if(endIndex >= searchFew.size()){
					if(this.subviews.contains(nextResults)) this.subviews.remove(nextResults);
					
				}
				if(!this.subviews.contains(previousResults)) this.subviews.add(previousResults);
			}
			
		}
	  }
     

